package cn.nawang.ebeim.server.thread;

import cn.nawang.ebeim.server.constants.Config;
import cn.nawang.ebeim.server.constants.Constant;
import cn.nawang.ebeim.server.entity.StorageVersionTreeToDo;
import cn.nawang.ebeim.server.queue.EbeimQueues;
import cn.nawang.ebeim.server.task.UploadFileTask;
import cn.nawang.ebeim.server.util.ApiVisitUtil;
import cn.nawang.ebeim.server.util.TransportUtil;

import java.io.File;

/**
 * Created by GanJc on 2015-11-11 10:38
 */
public class VersionTreeUploadFolderScanner extends FolderScanner {

    @Override
    protected String getScanPath() {
        return Config.WORKING_DIR + Config.VERSION_UPLOAD;
    }

    @Override
    protected String getFileType() {
        return Constant.FILE_TYPE_VERSION;
    }

    @Override
    protected void putIntoTaskQueue(UploadFileTask uploadFileTask) {
        try {
            EbeimQueues.uploadVersionTreeQueue.put(uploadFileTask);
            LOG.debug("将版本树文件放入上传队列:{}" , uploadFileTask );
        } catch (InterruptedException e) {
            LOG.error("将版本树文件放入上传队列失败:{}", e);
        }
    }

    @Override
    protected void redoBreakPointTask(String dsId, File file, String trueFileName, String tempFileName) {
        LOG.info("断点续传版本树：" + file.getName());
        StorageVersionTreeToDo storageVersionTreeToDo =  ApiVisitUtil.getStorageVersionTreeToDoByDsIdAndVersion(dsId, trueFileName);
        boolean renameToFlag = file.renameTo(new File(tempFileName));
        LOG.info("断点续传开始前重命名为临时文件结果：{} - " + renameToFlag, tempFileName);
        TransportUtil.downloadDataFile(dsId, trueFileName, storageVersionTreeToDo.getStorageCloudId1());
    }
}
